#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/10/25 15:05
# @Author  : 胡浩浩
# @File    : run_manage.py
# @IDE     : PyCharm
# @description :
import os
import time

import pytest
from pywebio.output import *
from pywebio.pin import *

from commons.common_tools import write_to_yml, get_ip_address
from src.api import background_tasks
from src.menus import show_menus

async def run_pytest(report_id):
    print("我被加入后台任务，我将会在后台被执行")
    allure_path = f'./allures/{report_id}/temp'
    await pytest.main(['-vs', f'--alluredir={allure_path}', '--clean-alluredir', '--cache-clear'])
    os.system(f"allure generate {allure_path} -o ./allures/{report_id}/report")
    pin_update("show_data", value=f"执行完成，测试报告地址：http://{get_ip_address()}:8055/reports/{report_id}/report")


def html_show_run_detail():
    test_url = pin.test_url
    product = pin.product
    username = pin.username
    password = pin.password
    port = pin.port
    print(test_url)
    print(product)
    print(username)
    print(port)
    if not test_url or not product or not username or not password or not port:
        toast_str = ""
        if not test_url:
            toast_str += "测试地址，"
        if not product:
            toast_str += "产品标识，"
        if not username:
            toast_str += "用户名，"
        if not password:
            toast_str += "密码，"
        if not port:
            toast_str += "收包端口，"
        toast(f"{toast_str}为必填字段，不能为空", color="error")
        return
    write_to_yml("config.yml", {"test_url": test_url, "product": product, "username": username, "password": password,"port":port})
    popup('执行中', [
        put_textarea("show_data"),
        put_row([
            None,
            put_button("关闭", onclick=lambda: close_popup(), color="dark")
        ], size="89%")
    ], size="large", closable=False)
    pin_update("show_data", value="执行中，请稍后")
    report_id = int(time.time() * 1000)
    background_tasks.add_task(run_pytest(report_id))



def html_show_run():
    """
    解析规则专项测试
    """
    show_menus()
    put_tabs([
        {
            "title": "内置解析规则测试",
            "content": [
                put_html("<hr>"),
                put_html("<h3>执行信息</h3>"),
                put_row([
                    None,
                    put_text("测试地址"),
                    put_input("test_url", value='https://192.168.114.91:8443',
                              placeholder="输入测试地址：例如：https://192.168.114.91:8443"),
                    None
                ], size="10% 10% 50%"),
                put_html("<br>"),
                put_row([
                    None,
                    put_text("产品标识"),
                    put_input("product", value='csa', placeholder="输入产品标识：例如：csa,mdc,sa"),
                    None,
                ], size="10% 10% 50%"),
                put_html("<br>"),
                put_row([
                    None,
                    put_text("用户名"),
                    put_input("username", value='sysadmin', placeholder="输入用户名"),
                    None,
                ], size="10% 10% 50%"),
                put_html("<br>"),
                put_row([
                    None,
                    put_text("密码"),
                    put_input("password", value='Sa_7654321', placeholder="输入密码"),
                    None,
                ], size="10% 10% 50%"),
                put_html("<br>"),
                put_row([
                    None,
                    put_text("udp收包端口"),
                    put_input("port", value='9514', placeholder="输入udp收包端口"),
                    None,
                ], size="10% 10% 50%"),
                put_html("<br>"),
                put_row([
                    None,
                    None,
                    None,
                    put_button("执行", onclick=lambda: html_show_run_detail()),
                ], size="10% 10% 55% 5%"
                )]
        }
    ], scope="data_list")
